obj-m += sync_counter.o

sync_counter-objs := entry.o init.o worker_thread.o \
	timer.o fifo.o task.o \
	mock.o serial_file.o \
	trigger.o neli.o \
	gpio.o irq.o pedal.o sync_source.o

PWD := $(CURDIR)

ccflags-y := -fmacro-prefix-map="$(M)"=@

MODULE_NAME := sync_counter

FAMILY_NAME := sync-counter

KERNEL_BASE := /lib/modules/$(shell uname -r)

BUILD_DIR := $(KERNEL_BASE)/build


COMMAND_GEN := scripts/clang-tools/gen_compile_commands.py

MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR := $(dir $(MAKEFILE_PATH))

.PHONY: compile_commands all clean press

all:
	make -C $(BUILD_DIR) M=$(PWD) modules

clean:
	make -C $(BUILD_DIR) M=$(PWD) clean

compile_commands: all
	@if [ -e $(KERNEL_BASE)/source ]; \
	then $(KERNEL_BASE)/source/$(COMMAND_GEN) $(MAKEFILE_DIR) -d $(BUILD_DIR); \
	else $(KERNEL_BASE)/build/$(COMMAND_GEN) $(MAKEFILE_DIR) -d $(BUILD_DIR); \
	fi

press:
	@echo "press pedal, use enter to release"
	python3 press.py


insmod: all
	- stty -F /dev/ttyACM0 9600 cs8 -cstopb -parenb raw -echo
	sudo insmod sync_counter.ko use_mock=1

deploy: all
	sudo insmod sync_counter.ko

rmmod:
	sudo rmmod sync_counter

watchlog:
	dmesg -f kern -T -w | grep "[<$(MODULE_NAME)>]"

install: all
	- mkdir $(KERNEL_BASE)/local
	cp sync_counter.ko $(KERNEL_BASE)/local
	depmod -a
